package com.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
//        out.println("当前页面在LoginServlet");
        //获取来自登录页面的用户名和密码
        String uname = req.getParameter("uname1");
        String pwd = req.getParameter("pwd1");
        if(uname !=null && uname.length()>0 && pwd !=null && pwd.length()>0) {
            //将来自注册页面的用户名和密码与登陆页面的用户名和密码相比较
            //获取来自注册页面的用户名和密码
            HttpSession session = req.getSession();
            String usernameFromRegister =  (String) session.getAttribute("username");
            String pwdFromRegister = (String) session.getAttribute("password");
            if (uname.equals(usernameFromRegister) && pwd.equals(pwdFromRegister)) {
                out.println(uname +":你很厉害，当前登录的用户名和密码与注册时的用户名和密码是一致的");
            } else {
                out.println(uname +",当前登录的用户名和密码与注册时的用户名和密码是不一致的");
            }
        } else {
            out.println("当前的用户名和密码为空");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
